import pytest
import jsonschema

import config
from api.ihrm_login_api import IhrmLoginApi
from common.assert_util_ihrm import common_assert
from common.read_json_util_ihrm import read_json_data


# 测试用例层
class TestIhrmLogin(object):
    path_filename = config.BASE_PATH + "/data/ihrm_data.json"

    # 测试方法 - 登录成功
    @pytest.mark.parametrize(argnames="desc, req_data, status_code, success, code, message",
                             argvalues=read_json_data(path_filename))
    def test_login(self, desc, req_data, status_code, success, code, message):
        # 调用 自己封装 loin 接口.
        json_data = req_data
        resp = IhrmLoginApi.login(json_data=json_data)
        print("\n测试用例" + desc, resp.json())

        # 断言
        # common_assert(resp=resp, status_code=status_code, success=success, code=code, message=message)

        # 断言 响应状态码
        assert 200 == resp.status_code

        # 使用全量字段校验, 替换 断言
        # 校验规则
        schema = {
            "type": "object",
            "properties": {
                "success": {"const": True},
                "code": {"const": 10000},
                "message": {"pattern": "操作成功"},
                "data": {"type": "string"}
            },
            "required": ["success", "code", "message", "data"]
        }
        jsonschema.validate(instance=resp.json(), schema=schema)
